package com.jbookshelf.beans;

import java.util.List;

import com.jbookshelf.dal.entities.Permissions;

import org.primefaces.context.RequestContext;
import org.primefaces.model.DualListModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.jbookshelf.dal.providers.PermissionsDataProvider;

@Component
@Scope("request")
public class PermissionsBean {

	private DualListModel<Permissions> permissions;

	@Autowired
	private PermissionsDataProvider dataProvider;

	public DualListModel<Permissions> getPermissions() {
		if (permissions == null) {
			permissions = dataProvider.getPermissions();
		}
		return permissions;
	}

	public void setPermissions(DualListModel<Permissions> permissions) {
		this.permissions = permissions;
	}

	public void editPermissions(Integer id) {

		permissions = dataProvider.getPermissions(id);

		final RequestContext context = RequestContext.getCurrentInstance();
		context.execute("permissionsdlg.show()");

	}

	public void saveUserPermissions(List<Permissions> list, int userId) {

		dataProvider.setPermissions(list, userId);

		final RequestContext context = RequestContext.getCurrentInstance();
		context.execute("permissionsdlg.show()");

	}

}
